<?php
/*Avec ce fichier, je peut utiliser le url helper dans cette class!*/

class documentationActions extends sfActions
{
	function getClassName($request){  	
  		return 'documentation';
  	}	
	  function getModuleName(){
	  	return 'documentation';
	  }	
  
	public function executeIndex(sfWebRequest $request)
	{
		$this->objDef = new objectDef('documentation');
    	$this->module = $this->getModuleName();
	}
	
	
	public function executeShow(sfWebRequest $request)
	{
	 	$this->self = Doctrine::getTable($this->getClassName($request))->find($request->getParameter('id'));
	    $this->forward404Unless($this->self);
		$this->className = $this->getClassName($request);
		$objDef = new objectDef($this->className);
		
		$this->module = $this->getModuleName();
		$this->objDef = $objDef;     
	    $this->owner = $this->getUser()->getAttribute('ownerId');
		$this->ownerClass = $this->getUser()->getAttribute('ownerClass');	
	}
	
	public function executeRenderDocumentationList(sfWebRequest $request)
	{
		$util = new Util();
		$className = 'documentation';
		$hide = array();
		$add = array();

		$orderByCol = $request->getParameter('order');
		$page = (int)$request->getParameter('page',1);

		$objDef = new objectDef($className);
		$descCol = $objDef->descriptor;
		$sf_user = $this->getUser();
		if($sf_user->isAuthenticated()){
			$roles = $sf_user->getRoles();	
		}else{
			$roles = array();
		}
		
		$q = DQ::create()
    		->select("r.*")
    		->from('documentation r')
    		->leftJoin('r.section s')
    		->addSelect('s.description')
    		->leftJoin('r.roles ro')
    		->leftJoin('r.personnes p');
    	if(!$sf_user->isSuperAdmin())
    	{
	    	if(count($roles)==0){
		    	$q->andWhere('ro.id is null');
	    	}else{
		    	$q->andWhereIn('ro.id ',array_keys($roles));
		    	$q->orWhere('ro.id is null');
		    	$q->whereParenWrap();
	    	}
	
	    	if($sf_user->isAuthenticated()){
	    	$personne=$sf_user->getUserProfile();
		    	if($personne)
		    	{
		    		$q->andWhere('(personne_id= ?', $personne->id);
		    		$q->orWhere('p.personne_id is null)');	
		    	}else
		    	{
		    		$q->addWhere('p.personne_id=0');
		    	}
	    	}
    	}
    	$q->addOrderBy('s.description');
    	$q->addOrderBy('r.title');
    	
    	$search = new searchBar();
		$search->addFilterstoQuery($q);
    	$q->setHydrationMode(Doctrine_Core::HYDRATE_ARRAY);
		$records = $q->execute();
		return $this->renderPartial('listDocumentation', array('records'=>$records , 'objDef'=>$objDef));
	}	
}